package com.thti.mecool.client.model.biz;

import java.util.List;

import android.content.Context;

import com.thti.mecool.client.db.DataBase;
import com.thti.mecool.client.model.po.Task;

public class TaskBizImpl {

	private DataBase dataBase; 

	public TaskBizImpl(Context context) {
		dataBase = new DataBase(context);
	}

	public void save(Task task) {
		String sqlString = "insert into task(userId,projectId,taskId,taskName,startTime,endTime,storeId,storeName,planId,isType,isFinish) values(?,?,?,?,?,?,?,?,?,?,?)";
		dataBase.execObject(
				sqlString,
				new Object[] { task.getUserId(), task.getProjectId(),
						task.getTaskId(), task.getTaskName(),
						task.getStartTime(), task.getEndTime(),
						task.getStoreId(), task.getStoreName(),
						task.getPlanId(),task.getIsType(),0});
		dataBase.close();
	}

	public void delete(String projectId, String taskId) {
		String sqlString = "delete from  task where projectId=? and taskId=?";
		dataBase.execObject(sqlString, new String[] { projectId, taskId });
		dataBase.close();
	}

	public void delete(String id) {
		String sqlString = "delete from  task where id=?";
		dataBase.execObject(sqlString, new String[] { id });
		dataBase.close();
	}

	@SuppressWarnings("unchecked")
	public List<Task> findAll(String planId) {

		String sql = "select  p.projectName,t.* from  task t,project p where t.planId=? AND t.planId=p.planId AND t.projectId=p.projectId order by t.startTime";
		List<Task> list = dataBase.findAll(Task.class, sql,
				new String[] { planId });
		dataBase.close();
		return list;
	}
	
	public boolean taskFinish(String planId,String taskId,String projectId,String userId){
		String sql="update task set isFinish=1 where planId=? AND taskId=? AND projectId=? AND userId=?";
		boolean finishOK=dataBase.execObject(sql, new String[]{planId,taskId,projectId,userId});
		dataBase.close();
		return finishOK;
	}
	
	@SuppressWarnings("unchecked")
	public List<Task> findByprojectId(String planId,String isType,int isFinish,String userId) {
		List<Task> list=null; 
		if(isType!=null){
		String sql = "select  p.projectName,t.* from  task t,project p where t.planId=? AND t.planId=p.planId AND  t.isType=? AND t.isFinish=? AND  t.userId=? AND t.projectId=p.projectId order by t.startTime";
             list = dataBase.findAll(Task.class, sql,
				new String[]{planId,isType,String.valueOf(isFinish),userId});
	    }else{
	    	String sql = "select  p.projectName,t.* from  task t,project p where t.planId=? AND t.planId=p.planId AND t.isType is NULL AND t.isFinish=? AND t.userId=? AND t.projectId=p.projectId order by t.startTime";
		    list = dataBase.findAll(Task.class, sql,new String[]{planId,String.valueOf(isFinish),userId});
	    }
		dataBase.close();
		return list;
	}
	public Task findByTask(String projectId,String planId,String taskId,String userId){
		String sql="select * from task where projectId=? and planId=? and taskId=?";
		Task task=(Task) dataBase.findByObject(Task.class, sql, new String[]{projectId,planId,taskId,userId});
		dataBase.close();
		return task;
	}
	@SuppressWarnings("unchecked")
	public List<Task> findByPlan(String planId,String userId) {
		
		String sql = "select  p.projectName,t.* from  task t,project p where t.isType='local' AND  t.planId=? AND t.userId=? AND t.projectId=p.projectId AND  t.planId=p.planId order by t.startTime";
		List<Task> list = dataBase.findAll(Task.class, sql,new String[]{planId,userId});
		dataBase.close();
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public List<Task> findTaskDetail(String date,String userId){
		String sql="select  p.projectName,t.* from  task t,project p where t.endTime<? AND t.userId=? AND (t.isType is NULL OR t.isType='assignment') AND t.projectId=p.projectId AND   t.planId=p.planId order by t.startTime AND t.isType ISNULL";
		List<Task> list = dataBase.findAll(Task.class, sql,new String[]{date,userId});
		dataBase.close();
		return list;
	}

	
	public void deletePlanId(String planId) {
		String sqlString = "delete from  task where planId=?";
		dataBase.execObject(sqlString, new String[] { planId });
		dataBase.close();
	}

}
